﻿using System.IO;

namespace LS.Screening.UI.Common
{
    public static class AppHelper
    {
        private static string executeDirectory = null;

        private static string workDirectory = null;

        private static string configDirectory = string.Empty;

        private static string logDirectory = string.Empty;

        private static string uploadFileDirectory = string.Empty;

        /// <summary>
        /// 执行目录
        /// </summary>
        public static string ExecuteDirectory
        {
            get
            {
                if (string.IsNullOrEmpty(executeDirectory))
                {
                    executeDirectory = AppDomain.CurrentDomain.BaseDirectory;
                }

                return executeDirectory;
            }
        }

        /// <summary>
        /// 工作目录(对于移动端，由移动端的launcher启动入口来指定)
        /// </summary>
        public static string WorkDirectory
        {
            get
            {
                if (workDirectory == null)
                {
                    workDirectory = ExecuteDirectory;
                }

                return workDirectory;
            }
        }

        /// <summary>
        /// 应用扩展缓存目录(移动端时使用)
        /// </summary>
        public static string ExternalCacheDir { get; set; }

        /// <summary>
        /// 配置目录
        /// </summary>
        public static string ConfigDirectory
        {
            get
            {
                if (string.IsNullOrEmpty(configDirectory))
                {
                    configDirectory = GetFolder("Config");
                }
                return configDirectory;
            }
        }

        /// <summary>
        /// 日志目录
        /// </summary>
        public static string LogDirectory
        {
            get
            {
                if (string.IsNullOrEmpty(logDirectory))
                {
                    if (!string.IsNullOrEmpty(ExternalCacheDir))
                    {
                        logDirectory = GetFolderByParent(ExternalCacheDir, "Logs");
                    }
                    else
                    {
                        logDirectory = GetFolderByParent(WorkDirectory, "Logs");
                    }
                }

                return logDirectory;
            }
        }

        /// <summary>
        /// 本地文件目录
        /// </summary>
        public static string UploadFileDirectory
        {
            get
            {
                return uploadFileDirectory;
            }
        }

        /// <summary>
        /// 临时文件目录
        /// </summary>
        public static string TempDirectory => GetFolder("Temp");

        /// <summary>
        /// 获取运行目录/基目录[WorkDirectory]下指定的目录，如果不存在，则会创建
        /// </summary>
        /// <param name="folders"></param>
        /// <returns></returns>
        public static string GetFolder(params string[] folders)
        {
            string path = Path.Combine(folders);
            path = Path.Combine(WorkDirectory, path);
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }

            return path;
        }

        /// <summary>
        /// 获取给定目录[参数:parent]下指定的目录，如果不存在，则会创建
        /// </summary>
        /// <param name="parent"></param>
        /// <param name="folders"></param>
        /// <returns></returns>
        public static string GetFolderByParent(string parent, params string[] folders)
        {
            string path = Path.Combine(folders);
            path = Path.Combine(parent, path);
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            return path;
        }
    }
}
